package main

import "time"

const (
	RUDDER_SENSOR_ANGLE_KEY = "rudderSensorAngle"

	RUDDER_HEADING_SET_KEY   = "rudderHeadingSet"
	RUDDER_SLEW_RATE_KEY     = "rudderSlewRate"
	RUDDER_COMMAND_ANGLE_KEY = "rudderCommandAngle"
	RUDDER_PUMP_OP_MODE_KEY  = "rudderPumpOpMode"
	RUDDER_COMMAND_MODE      = "rudderCommandMode"

	RUDDER_ALARM_OVERLOAD_NO1         = "rudderAlarmOverloadNo1"       // 1
	RUDDER_ALARM_PHASE_LOST_NO1       = "rudderAlarmPhaseLostNo1"      // 2
	RUDDER_ALARM_POWER_LOST_NO1       = "rudderAlarmPowerLstNo1"       // 3
	RUDDER_ALARM_LOW_LEVEL_NO1        = "rudderAlarmLowLevelNo1"       // 4
	RUDDER_ALARM_FILTER_BLOCK_NO1     = "rudderAlarmFilterBlockNo1"    // 5
	RUDDER_ALARM_HIGH_OIL_TEMP_NO1    = "rudderAlarmHighOilTempNo1"    // 6
	RUDDER_ALARM_LOW_OIL_PRESSURE_NO1 = "rudderAlarmLowOilPressureNo1" // 7
	RUDDER_ALARM_GROUND_FAILURE       = "rudderAlarmGroundFailure"     // 8
	RUDDER_ALARM_HYDRAULIC_BLOCK_NO1  = "rudderAlarmHydraulicBlockNo1" // 9
	RUDDER_ALARM_OVERLOAD_NO2         = "rudderAlarmOverloadNo2"       // 10
	RUDDER_ALARM_PHASE_LOST_NO2       = "rudderAlarmPhaseLostNo2"      // 11
	RUDDER_ALARM_POWER_LOST_NO2       = "rudderAlarmPowerLstNo2"       // 12
	RUDDER_ALARM_LOW_LEVEL_NO2        = "rudderAlarmLowLevelNo2"       // 13
	RUDDER_ALARM_FILTER_BLOCK_NO2     = "rudderAlarmFilterBlockNo2"    // 14
	RUDDER_ALARM_HIGH_OIL_TEMP_NO2    = "rudderAlarmHighOilTempNo2"    // 15
	RUDDER_ALARM_LOW_OIL_PRESSURE_NO2 = "rudderAlarmLowOilPressureNo2" // 16
	RUDDER_ALARM_OFFSET               = "rudderAlarmOffset"            // 17
	RUDDER_ALARM_HYDRAULIC_BLOCK_NO2  = "rudderAlarmHydraulicBlockNo2" // 18
	RUDDER_ALARM_OP_POWER_LOST_NO1    = "rudderAlarmOpPowerLostNo1"    // 19
	RUDDER_ALARM_OP_POWER_LOST_NO2    = "rudderAlarmOpPowerLostNo2"    // 20
	RUDDER_ALARM_EMERG_POWER_LOST     = "rudderEmergPowerLost"         // 21
	RUDDER_ALARM_DRIFT                = "rudderAlarmDrift"             // 22
	RUDDER_ALARM_CLOSE_LOOP           = "rudderAlarmCloseLoop"         // 23
	RUDDER_ALARM_AUTO_POWER           = "rudderAlarmAutoPower"         // 24
)

var alarms = []struct {
	key string
}{
	{
		key: RUDDER_ALARM_OVERLOAD_NO1,
	},
	{
		key: RUDDER_ALARM_PHASE_LOST_NO1,
	},
	{
		key: RUDDER_ALARM_POWER_LOST_NO1,
	},
	{
		key: RUDDER_ALARM_LOW_LEVEL_NO1,
	},
	{
		key: RUDDER_ALARM_FILTER_BLOCK_NO1,
	},
	{
		key: RUDDER_ALARM_HIGH_OIL_TEMP_NO1,
	},
	{
		key: RUDDER_ALARM_LOW_OIL_PRESSURE_NO1,
	},
	{
		key: RUDDER_ALARM_GROUND_FAILURE,
	},
	{
		key: RUDDER_ALARM_HYDRAULIC_BLOCK_NO1,
	},
	{
		key: RUDDER_ALARM_OVERLOAD_NO2,
	},
	{
		key: RUDDER_ALARM_PHASE_LOST_NO2,
	},
	{
		key: RUDDER_ALARM_POWER_LOST_NO2,
	},
	{
		key: RUDDER_ALARM_LOW_LEVEL_NO2,
	},
	{
		key: RUDDER_ALARM_FILTER_BLOCK_NO2,
	},
	{
		key: RUDDER_ALARM_HIGH_OIL_TEMP_NO2,
	},
	{
		key: RUDDER_ALARM_LOW_OIL_PRESSURE_NO2,
	},
	{
		key: RUDDER_ALARM_OFFSET,
	},
	{
		key: RUDDER_ALARM_HYDRAULIC_BLOCK_NO2,
	},
	{
		key: RUDDER_ALARM_OP_POWER_LOST_NO1,
	},
	{
		key: RUDDER_ALARM_OP_POWER_LOST_NO2,
	},
	{
		key: RUDDER_ALARM_EMERG_POWER_LOST,
	},
	{
		key: RUDDER_ALARM_DRIFT,
	},
	{
		key: RUDDER_ALARM_CLOSE_LOOP,
	},
	{
		key: RUDDER_ALARM_AUTO_POWER,
	},
}

type cache struct {
	data map[string]entry
}

type entry struct {
	Value     interface{}
	Timestamp time.Time
	Static    bool
}

func (c *cache) set(m map[string]entry) error {
	for k, v := range m {
		err := c.setEntry(k, v)
		if err != nil {
			return err
		}
	}
	return nil
}

func (c *cache) setEntry(key string, v entry) error {
	if c.data == nil {
		c.data = map[string]entry{}
	}
	c.data[key] = v
	return nil
}

func (c *cache) getAll() map[string]entry {
	return c.data
}
